package com.attendcollatesys.service.impl;


import com.attendcollatesys.entity.ATDayDetails;
import com.attendcollatesys.entity.vo.ATDayDetailsVO;
import com.attendcollatesys.ex.handler.ex.MessageNullValueException;
import com.attendcollatesys.mapper.ATDayDetailMapper;
import com.attendcollatesys.service.ATDayDetailsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AtDayDetailsImplService implements ATDayDetailsService {
    @Autowired
    private ATDayDetailMapper atDayDetailMapper;

    @Override
    public int insertAll(List<ATDayDetails> list) {
        return atDayDetailMapper.insertBatch(list);

    }

    @Override
    public int deleteByDayRtxList(List<String> list) {
        return atDayDetailMapper.deleteByDayRtxList(list);
    }

    @Override
    public Map<String, Object> queryATDayDetailsList(Map<String, Object> params) throws MessageNullValueException {
        String startDate = params.get("startDate") == null ? "":(String) params.get("startDate");
        String endDate = params.get("endDate") == null ? "":(String) params.get("endDate");
        String name = params.get("name")== null ? "":(String) params.get("name");
        String rtxNumber = params.get("rtxNumber")== null ? "":(String) params.get("rtxNumber");
        int pageNum = (int) params.get("pageNum");
        int pageSize = (int) params.get("pageSize");


        PageHelper.startPage(pageNum, pageSize);

        List<ATDayDetails> allData = atDayDetailMapper.selectOneDetailByDateAndRtx(startDate,endDate,name,rtxNumber);
        if( allData.size() == 0 ){
           throw new MessageNullValueException("当前条件下无数据！");
        }
        //转化日期格式
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat dateFormatss = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat dateFormatq = new SimpleDateFormat("yyyyMM");
        List<ATDayDetailsVO> allReturnData = new ArrayList<>();
        PageInfo<ATDayDetails> pageInfo = new PageInfo<>(allData);
        for (ATDayDetails allDatum : allData) {
            ATDayDetailsVO atDayDetailsVO = new ATDayDetailsVO();
            atDayDetailsVO.setSerialNo(allDatum.getSerialNo());


            if (allDatum.getAttendanceDate() != null && !"".equals(allDatum.getAttendanceDate())){
                atDayDetailsVO.setAttendanceDate(dateFormatq.format(allDatum.getAttendanceDate()));
            }else {
                atDayDetailsVO.setAttendanceDate("");
            }

            atDayDetailsVO.setDateType(allDatum.getDateType());

            if (allDatum.getAttendanceDay() != null && !"".equals(allDatum.getAttendanceDay())){
                atDayDetailsVO.setAttendanceDay(dateFormat.format(allDatum.getAttendanceDay()));
            }else {
                atDayDetailsVO.setAttendanceDay("");
            }

            atDayDetailsVO.setSectionMID(allDatum.getSectionMID());
            atDayDetailsVO.setName(allDatum.getName());
            atDayDetailsVO.setRtxNumber(allDatum.getRtxNumber());
            atDayDetailsVO.setCardInformation(allDatum.getCardInformation());
            atDayDetailsVO.setCheckInTime(allDatum.getCheckInTime());

            if (allDatum.getSignOutDate() != null && !"".equals(allDatum.getSignOutDate())){
                atDayDetailsVO.setSignOutDate(dateFormat.format(allDatum.getSignOutDate()));
            }else {
                atDayDetailsVO.setSignOutDate("");
            }

            atDayDetailsVO.setSignOutTime(allDatum.getSignOutTime());
            atDayDetailsVO.setLeaveApplication(allDatum.getLeaveApplication());
            atDayDetailsVO.setLeaveType(allDatum.getLeaveType());
            atDayDetailsVO.setLeaveReason(allDatum.getLeaveReason());
            atDayDetailsVO.setOvertimeApplication(allDatum.getOvertimeApplication());
            atDayDetailsVO.setOvertimeContent(allDatum.getOvertimeContent());


            if (allDatum.getEffectiveStateTime() != null && !"".equals(allDatum.getEffectiveStateTime())){
                atDayDetailsVO.setEffectiveStateTime(dateFormatss.format(allDatum.getEffectiveStateTime()));
            }else{
                atDayDetailsVO.setEffectiveStateTime("");
            }

            if (allDatum.getEffectiveEndTime() != null && !"".equals(allDatum.getEffectiveEndTime())){
                atDayDetailsVO.setEffectiveEndTime(dateFormatss.format(allDatum.getEffectiveEndTime()));
            }else {
                atDayDetailsVO.setEffectiveEndTime("");
            }

            atDayDetailsVO.setExpectedWorkHours(allDatum.getExpectedWorkHours());
            atDayDetailsVO.setAbsenceWorkHours(allDatum.getAbsenceWorkHours());
            atDayDetailsVO.setAbsentWorkHours(allDatum.getAbsentWorkHours());
            atDayDetailsVO.setLateTimes(allDatum.getLateTimes());
            atDayDetailsVO.setLateWorkHours(allDatum.getLateWorkHours());
            atDayDetailsVO.setOvertimeWorkHours(allDatum.getOvertimeWorkHours());
            atDayDetailsVO.setAttendanceHours(allDatum.getAttendanceHours());
            allReturnData.add(atDayDetailsVO);
        }



        Map<String,Object> tReturn = new HashMap<>();
        tReturn.put("total",pageInfo.getTotal());
        tReturn.put("dailyAtteSheet",allReturnData);

        return tReturn;
    }
}
